define([
    "UtilDir/util",
    "UtilDir/tool",
    "UtilDir/searchBlock",
    "static/app/fwsjcj/analysis/analysisEcharts",
    "EasyUI", "EasyUI-lang"
], function (Util, Tool, SearchBlock, Echarts) {

    var sysPath = getServer() + "/static/app/fwsjcj";

    var zwFormHtml = sysPath + "/brick/views/zwForm.html";
    var getZwController = function () {
        return getServer() + "/platform/org/zws/";
    };

    /**
     * 页面初始化
     */
    var init = function () {
        initSearchBlock();
        queryBtnBind();
        createProvince();
        createObjectAnalysis();
        createRegionalBuildingEcharts();
        clickTabs();
        window.addEventListener("resize", function () {
            var tabcontent_active = $('.tab-content').children('div.active');
            var tabcontent_div = tabcontent_active.children('div');

            for (var i = 0; i < tabcontent_div.length; i++) {
                var echarts_id = $(tabcontent_div[i]).attr('id');
                if (echarts_id) {
                    Echarts.resizeEcharts(echarts_id);
                }
            }
        })
    };
    var initSearchBlock = function () {
        SearchBlock.init("searchBlock");
    };
    var queryBtnBind = function () {
        $("#btnSearch").click(function () {
            debugger;
            // 请求参数
            var paramVo = {
                belongtoProvince: '',
                belongtoCity: '',
                belongtoArea: '',
                spType: ''
            }
            // 参数赋值
            paramVo.belongtoProvince = $('#province').combobox('getText');
            if ($('#city').combobox('getText') != "全部") {
                paramVo.belongtoCity = $('#city').combobox('getText');
            }
            if ($('#county').combobox('getText') != "全部") {
                paramVo.belongtoArea = $('#county').combobox('getText');
            }
            paramVo.spType = $('#objectAnalysis').combobox('getText');
            // 传参
            var tabid = $('#myTab').find("li.active a").attr('id');
            if (tabid == 'regionalBuildinglab') {
                createRegionalBuildingEcharts(paramVo);
            } else if (tabid == 'structTypelab') {
                createStructTypeEcharts(paramVo);
            } else if (tabid == 'ageBuildlab') {
                createAgeBuildEcharts(paramVo);
            } else if (tabid == 'earthquakelab') {
                createEarthquakeEcharts(paramVo);
            } else if (tabid == 'earthquakeSituationlab') {
                createEarthquakeSituationEcharts(paramVo);
            } else if (tabid == 'ruralHouselab') {
                createRuralHouseEcharts(paramVo);
            } else if (tabid == 'ruralEarthquakelab') {
                createRuralEarthquakeEcharts(paramVo);
            } else if (tabid == 'hiddenDangerlab') {
                createHiddenDangerEcharts(paramVo);
            }
        });
        $("#btnReset").click(function () {
            $('#province').combobox('clear');
            $('#city').combobox('loadData', {});//清空option选项
            $('#city').combobox('clear');
            $('#county').combobox('loadData', {});//清空option选项
            $('#county').combobox('clear');
            $("#objectAnalysis").combobox("clear");
        });
    };

    var clickTabs = function () {
        $('#myTab').on('shown.bs.tab', function (e) {
            var tabid = $(e.target).attr('id');
            if (tabid == 'regionalBuildinglab') {
                createRegionalBuildingEcharts();
            } else if (tabid == 'structTypelab') {
                createStructTypeEcharts();
            } else if (tabid == 'ageBuildlab') {
                createAgeBuildEcharts();
            } else if (tabid == 'earthquakelab') {
                createEarthquakeEcharts();
            } else if (tabid == 'earthquakeSituationlab') {
                createEarthquakeSituationEcharts();
            } else if (tabid == 'ruralHouselab') {
                createRuralHouseEcharts();
            } else if (tabid == 'ruralEarthquakelab') {
                createRuralEarthquakeEcharts();
            } else if (tabid == 'hiddenDangerlab') {
                createHiddenDangerEcharts();
            }
        })
    }

    var createProvince = function () {
        $('#province').combobox({
            url: "/app/divisions/root/subdivisions",
            method: 'get',
            editable: false, //不可编辑状态  
            cache: false,
            // panelHeight: 'auto',//自动高度适合  
            valueField: 'divisionId',
            textField: 'divisionName',
            onLoadSuccess: function (data) {
                $('#province').combobox('select', '');
            }
        });
        $("#province").combobox({
            onSelect: function (value) {
                var re = value.divisionId;
                var list = new Array();
                list.push({"divisionId": "-1", "divisionName": "全部"});
                if (re != null && re != '') {
                    list.push(re);
                }

                if (value.divisionName == '北京市' || value.divisionName == '天津市' || value.divisionName == '上海市' || value.divisionName == '重庆市') {
                    var division = [];
                    division.push(value);
                    $('#city').combobox('loadData', division);
                    $('#city').combobox('select', value.divisionName);

                    $('#county').combobox({
                        url: "/app/divisions/" + value.divisionId + "/subdivisions",
                        method: 'get',
                        editable: false, //不可编辑状态  
                        cache: false,
                        //panelHeight: 'auto',//自动高度适合  
                        valueField: 'divisionId',
                        textField: 'divisionName',
                        onLoadSuccess: function () {
                            $('#county').combobox('select', list[0].divisionName);
                        }
                    });
                } else {
                    $('#city').combobox({
                        url: "/app/divisions/" + value.divisionId + "/subdivisions",
                        method: 'get',
                        editable: false, //不可编辑状态  
                        cache: false,
                        // panelHeight: 'auto',//自动高度适合  
                        valueField: 'divisionId',
                        textField: 'divisionName',
                        onLoadSuccess: function () {
                            $('#city').combobox('select', list[0].divisionName);
                        }
                    });
                    createCity();
                    $('#county').combobox('loadData', {});//清空option选项
                    $('#county').combobox('clear');
                }
            }
        });

        createCity();
        $('#county').combobox({
            data: '',
            editable: false, //不可编辑状态  
            cache: false
        });
    }

    var createCity = function () {
        $("#city").combobox({
            onSelect: function (value) {
                var re = value.divisionId;
                var list = new Array();
                list.push({"divisionId": "-1", "divisionName": "全部"});
                if (re.list != null && re.list != 'null') {
                    list.push(re);
                }
                $('#county').combobox({
                    url: "/app/divisions/" + value.divisionId + "/subdivisions",
                    method: 'get',
                    editable: false, //不可编辑状态  
                    cache: false,
                    //panelHeight: 'auto',//自动高度适合  
                    valueField: 'divisionId',
                    textField: 'divisionName',
                    onLoadSuccess: function () {
                        $('#county').combobox('select', list[0].divisionName);
                    }
                });
            }
        });
    }

    var createObjectAnalysis = function () {
        //var objectList = [{id: 1, text: '审核通过'}, {id: 2, text: '质检通过'}]
        var objectList = [{id: 1, text: '审核通过','selected':true}]
        $('#objectAnalysis').combobox({
            data: objectList,
            editable: false, //不可编辑状态  
            cache: false,
            // panelHeight: 'auto',//自动高度适合  
            valueField: 'id',
            textField: 'text',
            onLoadSuccess: function (data) {
                 //$('#province').combobox('select', data[0].id);
                $('#objectAnalysis').combobox('setText','审核通过');
            }
        });
    }

    /* 地区建筑物总数统计echarts */
    var createRegionalBuildingEcharts = function (params) {
        console.log(params);

        /* 建筑栋数 */
        $.ajax({
            url: getServer() + "/fw/analysis/buildingNum",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                var buildings;
                buildings = data.data;

                Echarts.createBarEcharts({
                    id: "buildings",
                    x: buildings.x,
                    yname: '个数',
                    title: "栋数",
                    series: [{
                        title: '个数',
                        type: 'bar',
                        data: buildings.y,
                        force: {
                            repulsion: 300,
                            gravity: 0.003,
                            edgeLength: 140
                        }
                    }]
                });
                Echarts.resizeEcharts('buildings');
            }
        });

        /* 建筑面积 */
        $.ajax({
            url: getServer() + "/fw/analysis/buildingArea",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                var buildings;
                buildings = data.data;

                Echarts.createBarEcharts({
                    id: "buildingArea",
                    x: buildings.x,
                    title: "面积",
                    yname: '平方米',
                    series: [{
                        title: '个数',
                        type: 'bar',
                        data: buildings.y,
                        force: {
                            repulsion: 300,
                            gravity: 0.003,
                            edgeLength: 140
                        }
                    }]
                });
                Echarts.resizeEcharts('buildingArea');
            }
        });

    }

    /* 结构类型占比统计echarts */
    var createStructTypeEcharts = function (params) {
        // 城市
        $.ajax({
            url: getServer() + "/fw/analysis/buildingCity",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "cityBuildings",
                    legenddata: ['砖混', '底层混凝土柱-上部砖混', '钢筋混凝土', '砖木', '钢结构', '木结构', '其它'],
                    title: "结构类型占比统计",
                    subtitle: '城市',
                    series: [
                        {
                            name: '面积',
                            data: data.cityArea
                        },
                        {
                            name: '栋数',
                            data: data.cityNum
                        }
                    ]
                });
                Echarts.resizeEcharts('cityBuildings');
            }
        });
        // 农村
        $.ajax({
            url: getServer() + "/fw/analysis/buildingVillage",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "ruralBuildings",
                    legenddata: ['砖(砌块)混','混凝土结构(框架、剪力墙)','轻钢（钢）装配式','混凝土装配式','砖木','土木','石木','木结构','窑洞','混杂结构（砖/砌块/土/石）','其它'],
                    title: "结构类型占比统计",
                    subtitle: '乡村',
                    series: [
                        {
                            name: '面积',
                            data: data.villageArea
                        },
                        {
                            name: '栋数',
                            data: data.villageNum
                        }
                    ]
                });
                Echarts.resizeEcharts('ruralBuildings');
            }
        });
    }

    /* 建造年代占比统计echarts */
    var createAgeBuildEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/buildingTime",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "ageBuildings",
                    legenddata: ['70s以前', '70s', '80s', '90s', '00s', '10s'],
                    title: "建造年代占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('ageBuildings');
            }
        });
    }

    /* 抗震设防烈度占比统计echarts */
    var createEarthquakeEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/fortificationIntensity",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "earthquake",
                    legenddata: ['未设防', '6度', '7度', '8度', '9度', '10度'],
                    title: "抗震设防烈度占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('earthquake');
            }
        });
    }

    /* 抗震设防情况统计echarts */
    var createEarthquakeSituationEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/result",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "earthquakeSituation",
                    legenddata: ['未设防', '抗震设防能力不足', '基本符合抗震设防'],
                    title: "抗震设防情况统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('earthquakeSituation');
            }
        });
    }

    /* 农村居住房屋使用情况统计echarts */
    var createRuralHouseEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/houseUseSituation",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "ruralHouse",
                    legenddata: ['自用', '空置', '出租', '经营', '被征或待征', '其他'],
                    title: "农村居住房屋使用情况占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('ruralHouse');
            }
        });
    }

    /* 农村居住房屋抗震构造措施占比统计echarts */
    var createRuralEarthquakeEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/houseMeasures",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "ruralEarthquake",
                    legenddata: ['基本完备', '部分具备', '完全没有'],
                    title: "农村居住房屋抗震构造措施占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('ruralEarthquake');
            }
        });
    }

    /* 存在安全隐患占比统计echarts */
    var createHiddenDangerEcharts = function (params) {
        $.ajax({
            url: getServer() + "/fw/analysis/buildingIsDamage",
            contentType: "application/json",
            method: "GET",
            data: params,
            success: function (data) {
                console.log(data);

                Echarts.createPieEcharts({
                    id: "hiddenDanger",
                    legenddata: ['有', '无'],
                    title: "居住房屋有无明显可见的裂缝、变形、倾斜占比统计",
                    subtitle: '',
                    series: [
                        {
                            name: '面积',
                            data: data.area
                        },
                        {
                            name: '栋数',
                            data: data.num
                        }
                    ]
                });
                Echarts.resizeEcharts('hiddenDanger');
            }
        });
    }

    return {
        init: init
    };
});
